package cn.jbit.supermarker.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.jbit.supermarker.dao.BaseDao;
import cn.jbit.supermarker.dao.UserDao;
import cn.jbit.supermarker.entity.User;
import cn.jbit.supermarker.util.Page;


public class UserDaoImpl extends BaseDao implements UserDao {
	public static void main(String[] args) {
		UserDaoImpl userDaoImpl = new UserDaoImpl();
		User user = new User();
		Page page=new Page();
		// 添加
		// user.setName("李四");
		// user.setAddree("北京市朝阳区");
		// user.setGender(1);
		// user.setLoginnane("admin");
		// user.setLoginpwd("123");
		// user.setPhone("1234404");
		// user.setUser_type(0);
		// boolean flag=userDaoImpl.addUser(user);
		// 修改
		// user.setUser_id(3);
		// user.setName("刘能");
		// user.setLoginnane("liuneng");
		// user.setLoginpwd("456");
		// boolean flag= userDaoImpl.updateUser(user);
		
		//boolean flag = userDaoImpl.deleteUser(2);
//		List<User>list=userDaoImpl.getUserPageList(1, 4, "兰");
//		for (User user2 : list) {
//			System.out.println(user2.getUser_id()+user2.getName());
//		}
		//System.out.println(name.getName());
//		List<User> userList = userDaoImpl.getUserList();
//		for (User user2 : userList) {
//			System.out.println(user2.getName() + "\t" + user2.getLoginnane()
//					+ "\t" + user2.getLoginpwd());
//		}
//		if (flag) {
//			System.out.println("成功");
//		} else {
//			System.out.println("失败");
//		}
	}

	// 获得用户列表
	public List<User> getUserList() {
		List<User> userList = new ArrayList<User>();
		try {
			String sql = "SELECT *FROM smbms_user";
			Object[] params = {};
			rs = this.executeSQL(sql, params);
			while (rs.next()) {
				User user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setName(rs.getString("NAME"));
				user.setLoginnane(rs.getString("loginnane"));
				user.setLoginpwd(rs.getString("loginpwd"));
				user.setGender(rs.getInt("gender"));
				user.setBirthdate(rs.getString("birthdate"));
				user.setPhone(rs.getString("phone"));
				user.setAddree(rs.getString("addree"));
				user.setUser_type(rs.getInt("user_type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return userList;
	}

	// 添加用户信息
	public boolean addUser(User user) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("insert into smbms_user");
			sb.append("(NAME,loginnane,loginpwd,gender,birthdate,phone,addree,user_type)");
			sb.append("values(?,?,?,?,?,?,?,?)");
			Object[] params = {user.getName(), user.getLoginnane(),
					user.getLoginpwd(), user.getGender(), user.getBirthdate(),
					user.getPhone(), user.getAddree(), user.getUser_type() };
			int result = this.ExecuteUpdate(sb.toString(), params);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return true;
	}

	// 修改用户信息
	public boolean updateUser(User user) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("update smbms_user set NAME=?, loginnane=?, "
					+ "loginpwd=?, gender=?, birthdate=?, phone=?,addree=?,user_type=?   ");
			sb.append("   where user_id=?");
			Object[] params = { user.getName(), user.getLoginnane(),
					user.getLoginpwd(), user.getGender(), user.getBirthdate(),
					user.getPhone(), user.getAddree(), user.getUser_type(),
					user.getUser_id() };
			int result = this.ExecuteUpdate(sb.toString(), params);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return true;
	}

	// 删除用户信息
	public boolean deleteUser(int id) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("delete from smbms_user where user_id=?");
			Object[] params = {id};
			int result = this.ExecuteUpdate(sb.toString(), params);
			// 处理结果集
			if (result > 0) {
				return true;
			} else {
				return false;

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return true;

	}

	public User getUserById(int id) {
		User user = null;
		try {
			String sql = "select * from smbms_user where user_id=?";
			Object[] params = {id};
			ResultSet rs = this.executeSQL(sql, params);
			if (rs.next()) {
				user = new User();
				user.setAddree(rs.getString("addree"));
				user.setBirthdate(rs.getString("birthdate"));
				user.setGender(rs.getInt("gender"));
				user.setLoginnane(rs.getString("loginnane"));
				user.setLoginpwd(rs.getString("loginpwd"));
				user.setName(rs.getString("NAME"));
				user.setPhone(rs.getString("phone"));
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_type(rs.getInt("user_type"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return user;
	}
	//登录操作
	public User login(String loginName,String loginPwd) {
		User user = null;
		try {
			String sql = "select * from smbms_user where loginNane=? and loginPwd=?";
			Object[] params = {loginName,loginPwd };
			ResultSet rs = this.executeSQL(sql, params);
			if (rs.next()) {
				user = new User();
				user.setAddree(rs.getString("addree"));
				user.setBirthdate(rs.getString("birthdate"));
				user.setGender(rs.getInt("gender"));
				user.setLoginnane(rs.getString("loginnane"));
				user.setLoginpwd(rs.getString("loginpwd"));
				user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_type(rs.getInt("user_type"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return user;
	}
	
	/**
	 * 
	 * 
	 * 
	 */
	 
	public List<User> getUserPageList(int pageNo, int pageSize ,String userName) {
		List<User> userList = new ArrayList<User>();
		List<Object> params=new ArrayList<Object>();
		try {
			Page page = new Page();
			//User user=new User();
			User user=null;
			page.setCurrPageNo(pageNo);
			page.setPageSize(pageSize);
//			if(name!=null&&!"".equals(name)){
//			String sql = "SELECT * FROM smbms_user WHERE NAME LIKE '%"+name+"%' ORDER BY user_id ASC LIMIT ?,?";
//			Object[] params = {page.getStartRow(), pageSize };
//			rs = this.executeSQL(sql, params);
//			}else{ 
//			String sqlfenye="SELECT * FROM smbms_user ORDER BY user_id ASC LIMIT ?,?";
//			Object[] params = {page.getStartRow(), pageSize };
//			rs = this.executeSQL(sqlfenye, params);
//			}
			String sqlfenye="SELECT * FROM smbms_user WHERE 1=1 ";
			if(userName!=null&&!"".equals(userName)){
				sqlfenye+="   AND  NAME LIKE ? ";
				params.add("%"+userName+"%");
			}
			sqlfenye+="  ORDER BY user_id ASC LIMIT ?,?";
			params.add(page.getStartRow());
			params.add(pageSize);
			rs = this.executeSQL(sqlfenye, params.toArray());
			while (rs.next()) {
				user=new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setAddree(rs.getString("addree"));
				user.setBirthdate(rs.getString("birthdate"));
				user.setGender(rs.getInt("gender"));
				user.setLoginnane(rs.getString("loginnane"));
				user.setLoginpwd(rs.getString("loginpwd"));
				user.setName(rs.getString("NAME"));
				user.setPhone(rs.getString("phone"));
				user.setUser_type(rs.getInt("user_type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}

		return userList;
	}

	public int getTotalCount(String userName) {
		int totalCount = 0;
		try {
			List<Object> params=new ArrayList<Object>();
			String sql = "select count(*) from smbms_user WHERE 1=1";
			if(userName!=null&&!"".equals(userName)){
				sql+="  AND  NAME LIKE ?";
				params.add("%"+userName+"%");
			}
			ResultSet rs = this.executeSQL(sql, params.toArray());

			while (rs.next()) {
				totalCount = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			this.closeResource();
		}
		return totalCount;
	}

	@Override
	public User searchLogName(String userName) {
		User returnUser=null;
		try {
			String sql = "select * from smbms_user where loginNane=?";
			Object[] params = {userName};
			 rs = this.executeSQL(sql, params);
			 while(rs.next()){
				 returnUser=new User();
				 returnUser.setUser_id(rs.getInt("user_id"));
				 returnUser.setName(rs.getString("NAME"));
				 returnUser.setBirthdate(rs.getString("birthdate"));
				 returnUser.setLoginnane(rs.getString("loginnane"));
				 returnUser.setUser_type(rs.getInt("user_type"));
			 }
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return returnUser;
	}
}
